package com.ruoyi.web.controller.api.index;

import com.ruoyi.base.domain.BaseAbout;
import com.ruoyi.base.domain.BaseIndex;
import com.ruoyi.base.domain.BaseNoticeBar;
import com.ruoyi.base.service.IBaseAboutService;
import com.ruoyi.base.service.IBaseIndexService;
import com.ruoyi.base.service.IBaseNoticeBarService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.data.domain.DataUserRelation;
import com.ruoyi.data.service.IDataUserRelationService;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.service.ISysNoticeService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zzp
 * @version 1.0.0
 * @ClassName IndexController.java
 * @Description 首页接口
 * @createTime 2022年10月09日 16:05:00
 */
@Api("获取首页信息接口")
@RestController
@RequestMapping("/api/index")
public class IndexController extends BaseController {

    @Autowired
    private IBaseIndexService baseIndexService;

    @Autowired
    private IBaseNoticeBarService noticeBarService;

    @Autowired
    private ISysNoticeService noticeService;

    @Autowired
    private IBaseAboutService aboutService;

    @Autowired
    private IDataUserRelationService userRelationService;

    @Autowired
    private ISysUserService userService;

    @ApiOperation("获取首页视频")
    @GetMapping("/video")
    public AjaxResult video() {
        BaseIndex index = baseIndexService.selectBaseIndexUse();
        return AjaxResult.success(index);
    }

    @ApiOperation("获取首页图片")
    @GetMapping("/picList")
    public AjaxResult picList() {
        List<BaseIndex> picList = baseIndexService.selectBaseIndexImgList();
        return AjaxResult.success(picList);
    }

    @ApiOperation("获取首页轮播")
    @GetMapping("/indexList")
    public AjaxResult indexList() {
        BaseIndex index = new BaseIndex();
        index.setStatus("Y");
        List<BaseIndex> indexList = baseIndexService.selectBaseIndexList(index);
        return AjaxResult.success(indexList);
    }

    @ApiOperation("获取首页内容轮播")
    @GetMapping("/noticeBar")
    public AjaxResult noticeBar() {
        BaseNoticeBar noticeBar = noticeBarService.selectBaseNoticeBarUse();
        return AjaxResult.success(noticeBar);
    }

    @ApiOperation("获取关于我们")
    @GetMapping("/about")
    public AjaxResult about() {
        BaseAbout about = aboutService.selectBaseAboutUse();
        return AjaxResult.success(about);
    }

    @ApiOperation("获取首页通知公告")
    @GetMapping("/noticeList")
    public TableDataInfo noticeList(SysNotice notice) {
        startPage();
        List<SysNotice> list = noticeService.selectNoticeList(notice);
        return getDataTable(list);
    }

    @ApiOperation("获取通知公告信息")
    @GetMapping(value = "/noticeInfo/{noticeId}")
    public AjaxResult noticeInfo(@PathVariable Long noticeId) {
        return AjaxResult.success(noticeService.selectNoticeById(noticeId));
    }

    @ApiOperation("获取随机用户信息")
    @GetMapping(value = "/randomUser")
    public AjaxResult randomUser() {
        SysUser sysUser = userService.selectRandomUser();
        return AjaxResult.success(sysUser);
    }

    @ApiOperation("获取通知公告信息")
    @GetMapping(value = "/serviceInfo")
    public AjaxResult serviceInfo() {
        Long userId = getUserId();
        DataUserRelation userRelation;
        SysUser user;
        for (; ; ) {
            userRelation = userRelationService.selectDataUserRelationByUserId(userId);
            user = userService.selectUserById(userRelation.getTopUserId());
            if (!user.getUserLevel().equals("5")) {
                break;
            } else {
                userId = userRelation.getTopUserId();
                continue;
            }
        }
        return AjaxResult.success(user);
    }
}
